package com.zkca.xf.ui.activity.personprotect;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.zkca.xf.Constants_SP;
import com.zkca.xf.MyApplication;
import com.zkca.xf.R;
import com.zkca.xf.base.BaseTitleActivity;
import com.zkca.xf.model.javabean.JobLBean;
import com.zkca.xf.model.javabean.uploadbean.RequestInspect;
import com.zkca.xf.presenter.JobLPresenter;
import com.zkca.xf.ui.activity.nfc.Activity_saomiao;
import com.zkca.xf.ui.adapter.SingleAdapter;
import com.zkca.xf.ui.adapter.ViewHolder;
import com.zkca.xf.utils.DisplayUtil;
import com.zkca.xf.utils.LogUtil;
import com.zkca.xf.utils.PopWindowUtil;
import com.zkca.xf.utils.SPUtil;
import com.zkca.xf.utils.SpaceItemDecoration;
import com.zkca.xf.utils.TimeUtils;
import com.zkca.xf.utils.ToastUtils;
import com.zkca.xf.view.JobLView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class Job_LActivity extends BaseTitleActivity implements JobLView, View.OnClickListener {


    @BindView(R.id.xrv_task)
    XRecyclerView xrvTask;
    @BindView(R.id.wushuju)
    ImageView wushuju;
    @BindView(R.id.xre_dtype)
    XRecyclerView xreDtype;
    @BindView(R.id.shaixuan_begantime)
    TextView shaixuanBegantime;
    @BindView(R.id.shaixuanbeganImage)
    ImageView shaixuanbeganImage;
    @BindView(R.id.bgtime)
    RelativeLayout bgtime;
    @BindView(R.id.shaixuan_overtime)
    TextView shaixuanOvertime;
    @BindView(R.id.shaixuan_overimage)
    ImageView shaixuanOverimage;
    @BindView(R.id.ovtime)
    RelativeLayout ovtime;
    @BindView(R.id.shaixuan_cancel)
    TextView shaixuanCancel;
    @BindView(R.id.shaixuan_sure)
    TextView shaixuanSure;
    @BindView(R.id.drawerLayout)
    DrawerLayout drawerLayout;
    @BindView(R.id.beganxunjian)
    ImageView beganxunjian;

    private JobLPresenter jobLPresenter;
    private RequestInspect requestInspect;
    private LinearLayoutManager linearLayoutManager;
    int pageNum = 1;
    int pageSize = 10;

    private List<JobLBean.DataBean> dataBeanList = new ArrayList<>();
    private SingleAdapter<JobLBean.DataBean> adapter;

    private int taskId;
    private ImageView imgRight;
    View pop_selection_state;
    private TextView tv_pop_1, tv_pop_2, tv_pop_3;
    private int jobResult = 0;
    private Drawable dzx, ywc, gq, sx, wz;
    int taskStudes;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_task_l);
        ButterKnife.bind(this);
        jobLPresenter = new JobLPresenter(this, this);
        Intent intent = getIntent();
        taskId = intent.getIntExtra("taskId", -1);
//        if (intent.hasExtra("taskStudes")){
//           taskStudes  = intent.getIntExtra("taskStudes", -1);
//        }

        initAdapter();

        initMyview();

    }

    private void initAdapter() {
        adapter = new SingleAdapter<JobLBean.DataBean>(mContext, dataBeanList, R.layout.job_item) {
            @Override
            public void convert(ViewHolder holder, final JobLBean.DataBean dataBean, int position) {
                TextView jobNum_t = holder.getView(R.id.jobnum);
                TextView jobstatus_t = holder.getView(R.id.jobstatus);
                TextView jobUser_t = holder.getView(R.id.jobuser);
                TextView jobbegantime_t = holder.getView(R.id.jobbegantime);
                TextView jobovertime_t = holder.getView(R.id.jobovertime);
                TextView devicename_t = holder.getView(R.id.devicename);
                TextView devicepasion_t = holder.getView(R.id.devicepasion);

                final int jobId = dataBean.getJobId();//作业编号
                final int jobResult = dataBean.getJobResult();//作业完成状态
                long startTime = dataBean.getStartTime();//开始时间
                long endTime = dataBean.getEndTime();//结束时间
                String userName = dataBean.getUserName();//执行人员
                String devName = dataBean.getDevName();//设备名称
                String installPosition = dataBean.getInstallPosition();//安装地点
                final String starttime = TimeUtils.getFormatDate2(startTime);
                String endtime = TimeUtils.getFormatDate2(endTime);

                jobNum_t.setText(jobId + "");
                jobUser_t.setText(userName);
                devicename_t.setText(devName);
                devicepasion_t.setText(installPosition);
                jobbegantime_t.setText(starttime);
                jobovertime_t.setText(endtime);


                if (jobResult == 0) {
                    jobstatus_t.setText("未巡检");
                    jobstatus_t.setTextColor(Color.rgb(213, 66, 68));
                    jobstatus_t.setCompoundDrawablesWithIntrinsicBounds(dzx,
                            null, null, null);
                } else if (1 == jobResult) {
                    jobstatus_t.setText("已完成");
                    jobstatus_t.setTextColor(Color.rgb(41, 171, 145));
                    jobstatus_t.setCompoundDrawablesWithIntrinsicBounds(ywc,
                            null, null, null);
                } else if (2 == jobResult) {
                    jobstatus_t.setText("已过期");
                    jobstatus_t.setTextColor(Color.rgb(147, 146, 145));
                    jobstatus_t.setCompoundDrawablesWithIntrinsicBounds(gq,
                            null, null, null);
                } else if (3 == jobResult) {
                    jobstatus_t.setText("已失效");
                    jobstatus_t.setTextColor(Color.rgb(170, 169, 169));
                    jobstatus_t.setCompoundDrawablesWithIntrinsicBounds(sx,
                            null, null, null);
                } else {
                    jobstatus_t.setText("未知");
                    jobstatus_t.setTextColor(Color.rgb(226, 186, 49));
                    jobstatus_t.setCompoundDrawablesWithIntrinsicBounds(wz,
                            null, null, null);
                }

                holder.setItemViewOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent();
                        if (dataBean.getJobStatus() == 0) {
                            ToastUtils.show(mContext, "该作业尚未开始，开始时间为" + starttime);
                            return;
                        } else if (dataBean.getJobStatus() == 1) {
                            if (jobResult == 0) {
                                intent.setClass(mContext, Activity_saomiao.class);
                                intent.putExtra("TAG", "XUNJIAN");
                                intent.putExtra("jobbean", dataBean);
                                startActivity(intent);
                                finish();
                            } else if (jobResult == 1) {
                                intent.setClass(mContext, JobCompleteActivity.class);
                                intent.putExtra("TAG", "XUNJIAN");
                                intent.putExtra("jobbean", dataBean);
                                startActivity(intent);
                            }
                        } else if (dataBean.getJobStatus() == 2) {
                            if (jobResult == 0) {
                                ToastUtils.show(mContext, "该作业已过期 ！");
                                return;
                            } else if (jobResult == 1) {
                                intent.setClass(mContext, JobCompleteActivity.class);
                                intent.putExtra("TAG", "XUNJIAN");
                                intent.putExtra("jobbean", dataBean);
                                startActivity(intent);
                            }
                        }
                    }
                });
            }
        };
    }

    private void initDvData(int pagenum, int result) {
        Map map = new HashMap<String, Object>();
        map.put("taskId", taskId);
        map.put("jobResult", result + "");
        requestInspect = new RequestInspect();
        requestInspect.setPageNum(pagenum);
        requestInspect.setPageSize(pageSize);
        requestInspect.setParam(map);
        requestInspect.setAccessId(SPUtil.getString(this, Constants_SP.ACCESSID));
        requestInspect.setAction(4);
        requestInspect.setTimeStamp(TimeUtils.getTime());
        requestInspect.setSign(MyApplication.getInstance().generateDvSign(requestInspect));
        jobLPresenter.getJobL(requestInspect);
    }

    private void initMyview() {
        setTitleText("作业列表");
        setleftTextVisibility(true);
        setleftImgVisibility(true);
        setImgVisibility(true);
        imgRight = getImgRight();
        imgRight.setOnClickListener(this);
        pop_selection_state = LayoutInflater.from(this).inflate(R.layout.pop_taskstatues, null, false);
        tv_pop_1 = pop_selection_state.findViewById(R.id.tv_pop_1);
        tv_pop_2 = pop_selection_state.findViewById(R.id.tv_pop_2);
        tv_pop_3 = pop_selection_state.findViewById(R.id.tv_pop_3);
        tv_pop_1.setText("未巡检");
        tv_pop_2.setText("已完成");
        tv_pop_1.setOnClickListener(this);
        tv_pop_2.setOnClickListener(this);
        tv_pop_3.setOnClickListener(this);


        dzx = getResources().getDrawable(
                R.mipmap.weixunjian);//待执行
        ywc = getResources().getDrawable(
                R.mipmap.yiwancheng);//已完成
        gq = getResources().getDrawable(
                R.mipmap.gq);//已过期
        sx = getResources().getDrawable(
                R.mipmap.sx);//失效
        wz = getResources().getDrawable(
                R.mipmap.wz);//未知


        initDvData(pageNum, jobResult);
        linearLayoutManager = new LinearLayoutManager(mContext);
        xrvTask.setLayoutManager(linearLayoutManager);
        xrvTask.addItemDecoration(new SpaceItemDecoration(DisplayUtil.dip2px(
                mContext, 0.5f)));
        xrvTask.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                xrvTask.setLoadingMoreEnabled(true);
                dataBeanList.clear();
                pageNum = 1;
                initDvData(pageNum, jobResult);
            }

            @Override
            public void onLoadMore() {
                pageNum++;
                initDvData(pageNum, jobResult);
            }
        });
        xrvTask.setAdapter(adapter);
    }

    @Override
    public void showProgressDialog() {

    }

    @Override
    public void hideProgressDialog() {

    }

    @Override
    public void showError(String msg) {

    }

    @Override
    public void getJobL(JobLBean jobLBean) {
        xrvTask.refreshComplete();
        xrvTask.loadMoreComplete();
        if (jobLBean.getResult() == 0) {
            List<JobLBean.DataBean> data = jobLBean.getData();
            dataBeanList.addAll(data);

            if (data.size()>0){
                taskStudes = data.get(0).getJobStatus();
                int jobResult = data.get(0).getJobResult();
                LogUtil.e("taskStudes==",taskStudes+"jobResult="+jobResult);
                if (taskStudes==1&&jobResult==0){
                    beganxunjian.setVisibility(View.VISIBLE);
                }
            }
            if (jobLBean.getTotalpage() == pageNum) {
                xrvTask.setLoadingMoreEnabled(false);
            }
            if (pageNum == 1 && data.size() == 0) {
                wushuju.setVisibility(View.VISIBLE);
            } else {
                wushuju.setVisibility(View.GONE);
            }
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.im_base_right:
                PopWindowUtil.taskState(Job_LActivity.this, pop_selection_state, imgRight);
                break;
            case R.id.tv_pop_1:
                jobResult = 0;
                dataBeanList.clear();
                initDvData(pageNum, jobResult);
                PopWindowUtil.popupWindow.dismiss();
                break;
            case R.id.tv_pop_2:
                jobResult = 1;
                dataBeanList.clear();
                initDvData(pageNum, jobResult);
                PopWindowUtil.popupWindow.dismiss();
                break;
            case R.id.tv_pop_3:
                jobResult = 2;
                dataBeanList.clear();
                initDvData(pageNum, jobResult);
                PopWindowUtil.popupWindow.dismiss();
                break;
        }
    }

    @OnClick(R.id.beganxunjian)
    public void onViewClicked() {

        LogUtil.e("你猜是多少",taskStudes+"<>");
        if (taskStudes!=1){
            ToastUtils.show(mContext,"非执行中作业不可快速巡检");
        }else {
            Intent intent = new Intent();
            intent.setClass(mContext, Activity_saomiao.class);
            intent.putExtra("TAG", "F_XUNJIAN");
            intent.putExtra("taskId",taskId);
            startActivity(intent);
            finish();
        }

    }
}
